#include "Include.h"
#include "App.h"

int WINDOW_ID;
int WINDOW_WIDTH = 256;
int WINDOW_HEIGHT = 256;

std::shared_ptr<App> app;

void init()
{
    glClearColor(0, 0, 0, 0);
    
    app.reset(new App());
}

void keyboard(unsigned char key, int x, int y)
{
    switch( key )
    {
        case 27: glutDestroyWindow(WINDOW_ID); exit(0); break;
    }
}

void reshape(int width, int height)
{
    WINDOW_WIDTH = width;
    WINDOW_HEIGHT = height;
    glViewport(0,0,WINDOW_WIDTH,WINDOW_HEIGHT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0,WINDOW_WIDTH,WINDOW_HEIGHT,0,-1,1);
    glMatrixMode(GL_MODELVIEW);
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    app->update();
    app->draw();
    glutSwapBuffers();
}

void idle()
{
    glutPostRedisplay(); // tell the OS that window needs to be redrawn
}

int main(int argc, char *argv[]) // main entry point to program
{
    glutInit(&argc, argv); // initialise opengl
    glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE); // set window display mode (no alpha, double buffered)
    glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); // set initial window size (256,256)
    WINDOW_ID = glutCreateWindow("evowalker"); // create window with title (remember window id)
    glutKeyboardFunc(keyboard); // function to call when key pressed
    glutReshapeFunc(reshape); // function to call when window resized
    glutDisplayFunc(display); // function to kall when redrawing needed
    glutIdleFunc(idle); // function to call every frame
    init(); // set up our stuff (after opengl and window have been initialised)
    glutMainLoop(); // enter main opengl loop
    return 0; // should never reach here but just to be tidy
}

